<?php


namespace app\admin\controller;


use app\admin\common\library\FlowEngine;
use app\admin\model\AdminLog;
use app\admin\model\AuthGroup;
use app\admin\model\Budget;
use app\admin\model\Department;
use app\admin\model\FlowTask;
use app\admin\model\Item;
use app\admin\model\ItemLog;
use app\admin\model\ProjectBudget;
use app\admin\model\ProjectBudgetPlanConfig;
use app\admin\model\ProjectBudgetTypeConfig;
use app\admin\model\ProjectPlan;
use app\admin\model\Scheme;
use app\BaseController;
use think\facade\Db;
use think\facade\Event;
use think\facade\View;
use think\Request;
use function fast\e;

class Project extends BaseController
{
    protected $group = [];
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];

    public function initialize()
    {
        parent::initialize();
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
        $this->group = $this->auth->getGroups();//获取当前角色
        $group = Department::select();
        View::assign('group', $group);
    }

    //显示资源
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
            $result = Item::with(['admin', 'dept'])->paginate($limit, false, ['page' => $page])->each(function ($item, $key) {
                $item['progress_status'] = checkStatus($item['status']);
                $item['task_status'] = checkTaskStatus('projects', $item['id']);
            })->toArray();
            return json(['code' => 200, 'msg' => '', 'total' => $result['total'], 'data' => $result['data']]);
        }
        return View::fetch();
    }

    //新增
    public function add(Request $request)
    {
        $flow = \app\admin\model\Scheme::where('flowname', 'in', ['项目'])->select();
        if ($request->isAjax()) {
            $params = $request->only(['name', 'period', 'status', 'content', 'admin_id', 'group_id', 'flow_id']);
            $params['admin_id'] = $this->auth->id;
            if ($params) {
                $result = Item::create($params);
                if ($result) {
                    $params['project_id'] = $result->id;
                    $params['status'] = 0;
                    //插入预算表
                    Budget::create($params);
                    //start流程
                    $scheme = Scheme::find($params['flow_id']);
                    $serial_no = $this->getnumber($scheme['flowcode']);
                    $this->flow = new FlowEngine($scheme['flowcode']);
                    $config = [
                        'instancecode' => $serial_no,
                        'start_time' => date("Y-m-d H:i:s", time()),
                        'content' => $params['content'],
                        'from_id' => $result->id,
                        'from_table' => 'projects'
                    ];
                    $this->flow->start($config);
                    //end流程
                    $data = [
                        'name' => $request->param('name'),
                        'project_id' => $result->id,
                        'create_admin_id' => $this->auth->id,
                        'title' => '创建项目',
                        'content' => Item::PROGRESS_ONE,
                        'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                        'ip' => request()->ip()
                    ];
                    ItemLog::create($data);
                }
                return json(['code' => 200, 'url' => '', 'msg' => '项目创建成功！']);
            }
            return json(['code' => 400, 'url' => '', 'msg' => '项目创建失败！']);

        }
        View::assign('flow', $flow);
        return View::fetch();
    }

    //图片上传
    public function uploadImg(Request $request)
    {
        $file = request()->file('editormd-image-file');
        $savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file, 'uniqid');
        return json(['url' => '/storage/' . $savename, 'success' => 1, 'message' => '图片上传成功!']);

    }

    //编辑
    public function edit(Request $request, $id)
    {
        $model = Item::find($id);
        if ($request->isAjax()) {
            $params = $request->only(['id', 'name', 'period', 'status', 'content', 'admin_id', 'group_id']);
            $params['admin_id'] = $this->auth->id;
            if ($params) {
                Item::update($params);
                Budget::where('project_id', $params['id'])->update([
                    'name' => $params['name'],
                    'period' => $params['period'],
                    'content' => $params['content'],
                    'admin_id' => $params['admin_id'],
                    'group_id' => $params['group_id'],
                ]);
                return json(['code' => 200, 'url' => '', 'msg' => '项目更新成功！']);
            }
        }
        View::assign('model', $model);
        return View::fetch();
    }

    //查看内容
    public function DetailContent($id)
    {
        $project = Budget::find($id);
        if (is_null($project)) {
            return "找不到该项目";
        }
        $model = Item::find($project['project_id']);
        View::assign('model', $model);
        return View::fetch('detail');
    }

    //查看内容
    public function detail($id)
    {
        $model = Item::find($id);
        $group = AuthGroup::where(['level' => 2, 'status' => 1])->select();
        View::assign('group', $group);
        View::assign('model', $model);
        return View::fetch('detail');
    }

    //删除项目
    public function del($ids = "")
    {
        if ($ids) {
            $ids = explode(',', $ids);
            foreach ($ids as $key => $id) {
                $tasklist = FlowTask::where(['from_id' => $id, 'from_table' => 'projects'])->select();
                if (!empty($tasklist)) {
                    //删除对应的流程任务
                    FlowTask::where(['from_id' => $id, 'from_table' => 'projects'])->delete();
                }
            }
            $count = Item::destroy(array_values($ids));
            if ($count) {
                return json(['code' => 200, 'url' => '', 'msg' => '删除成功！']);
            }
        }
    }

    //项目审核页面
    public function examine(Request $request)
    {
        if ($request->isAjax()) {
            //获取每页显示的条数
            $limit = $request->param('limit');
            //获取当前页数
            $page = $request->param('page');
//            $where [] = array('group_id','=',$this->auth->dept_id);
            $result = Budget::with(['admin', 'AuthGroup'])->paginate($limit, false, ['page' => $page])->each(function ($item, $key) {
                $item['progress_status'] = checkStatus($item['status']);
                $item['task_status'] = checkTaskStatus('budget', $item['id']);
                $abc = Item::find($item['project_id']);
                if ($abc['status'] == 2) {
                    $item['budget_status'] = 1;
                }
            })->toArray();
            return json(['code' => 200, 'msg' => '', 'total' => $result['total'], 'data' => $result['data']]);
        }
        return View::fetch();
    }


    public function budget_add(Request $request)
    {
        $project = Item::select()->toArray();
        if ($request->isAjax()) {
            $data = $request->param();
            $res = Item::find($data['project_id']);
            $data['name'] = $res['name'];
            $data['admin_id'] = $res['admin_id'];
            $data['examine_admin_id'] = 0;
            $data['final_admin_id'] = 0;
            $data['group_id'] = $res['group_id'];
            $data['period'] = $res['period'];
            $data['content'] = $res['content'];
            $data['status'] = 0;
            $data['flow_id'] = $res['flow_id'];
            Budget::create($data);
            return json(['code' => 200, 'url' => '', 'msg' => '项目立项成功,请做项目预算！']);
        }
        View::assign('project', $project);
        return View::fetch();
    }

    //审核项目
    public function examineRead(Request $request, $id)
    {
        $flow = \app\admin\model\Scheme::where('flowname', 'in', ['预算'])->select();
        $model = Item::with(['ProjectBudget' => ['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig']])->find($id);
        if ($request->isAjax()) {
            $id = $request->param('id');
            $status = Item::update(['progress' => Item::PROGRESS_THREE, 'examine_admin_id' => $this->auth->id], ['id' => $id]);
            if ($status) {
                $data = [
                    'name' => $model->name,
                    'project_id' => $id,
                    'create_admin_id' => $this->auth->id,
                    'title' => Item::$progress[Item::PROGRESS_THREE],
                    'content' => Item::PROGRESS_THREE,
                    'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                    'ip' => request()->ip()
                ];
                ItemLog::create($data);
                return json(['code' => 200, 'url' => '', 'msg' => '项目立项成功,请做项目预算！']);
            }
        }

        View::assign('flow', $flow);
        View::assign('model', $model);
        return View::fetch('read');
    }

    //拒绝项目
    public function errorProject(Request $request)
    {
        if ($request->isAjax()) {
            $data = $request->param();
            $data['progress'] = Item::PROGRESS_TWO;
            $model = Item::find($request->param('id'));
            if ($data) {
                Item::update($data);
                $data = [
                    'name' => $model->name,
                    'project_id' => $request->param('id'),
                    'create_admin_id' => $this->auth->id,
                    'title' => Item::$progress[Item::PROGRESS_TWO],
                    'content' => Item::PROGRESS_TWO,
                    'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                    'ip' => request()->ip()
                ];
                ItemLog::create($data);
                return json(['code' => 200, 'url' => '', 'msg' => '项目撤回成功！']);
            }
            return json(['code' => 400, 'url' => '', 'msg' => '未知错误！']);
        }
    }

    //项目预算提交
    public function projectBudget(Request $request, $id)
    {
        $model = Budget::with('ExamineAdmin')->find($id);
        if (is_null($model)) {
            return "找不到该项目";
        }
        $dept = Department::find($model['group_id']);
        $admin = \app\admin\model\Admin::find($dept['manager']);
        $model['user_name'] = $admin['nickname'];
        $flow = \app\admin\model\Scheme::where('flowname', 'in', ['预算'])->select();
        $plan = ProjectPlan::where('status', 1)->select(); //查询计划
        if ($request->isAjax()) {
            //预算主表
            $data_budget = $request->only(['name', 'executive', 'execution_area', 'start_time', 'end_time', 'sample_num', 'flow_id']);
            $data_budget['executive_id'] = $model['examine_admin_id'];
            $data_budget['examine_admin_id'] = $this->auth->id; //审核人员
            $data_budget['project_id'] = $model['project_id'];
            if ($data_budget) {
                $project_budget = ProjectBudget::create($data_budget); //创建主表
            }

            //预算类型配置表
            $type_name = array_filter($request->param('type_name'));
            $main_sample_num = array_filter($request->param('main_sample_num'));
            $enlarge_sample_num = array_filter($request->param('enlarge_sample_num'));
            $type_config = new ProjectBudgetTypeConfig();
            $type_config_data = []; //组合新的数组


            foreach ($type_name as $key => $value) {
                $type_config_data[$key]['project_id'] = $model['project_id'];
                $type_config_data[$key]['project_budget_id'] = $project_budget->id; //项目预算id
                $type_config_data[$key]['type_name'] = $value ?? '';;
                $type_config_data[$key]['main_sample_num'] = $main_sample_num[$key] ?? '';;
                $type_config_data[$key]['enlarge_sample_num'] = $enlarge_sample_num[$key] ?? '';;
            }
            if ($type_config_data) {
                $type_config->saveAll($type_config_data);
            }
            //预算成本计划配置表
            $cost_id = array_filter($request->param('cost_id'));
            $num = array_filter($request->param('num'));
            $unit_price = array_filter($request->param('unit_price'));
            $sum_of_money = array_filter($request->param('sum_of_money'));
            $remark = array_filter($request->param('remark'));
            $plan_config = new ProjectBudgetPlanConfig();
            $plan_config_data = [];
            foreach ($cost_id as $key_play => $value_plan) {
                $plan_config_data[$key_play]['project_id'] = $model['project_id'] ?? '';;
                $plan_config_data[$key_play]['project_budget_id'] = $project_budget->id ?? '';;
                $plan_config_data[$key_play]['cost_id'] = $value_plan ?? '';;
                $plan_config_data[$key_play]['num'] = $num[$key_play] ?? '';;
                $plan_config_data[$key_play]['unit_price'] = $unit_price[$key_play] ?? '';
                $plan_config_data[$key_play]['sum_of_money'] = $sum_of_money[$key_play] ?? '';
                $plan_config_data[$key_play]['remark'] = $remark[$key_play] ?? '';
            }
            if ($plan_config_data) {
                $plan_config->saveAll($plan_config_data);
            }
            $data = [
                'name' => $model->name,
                'project_id' => $model['project_id'],
                'create_admin_id' => $this->auth->id,
                'title' => Item::$progress[Item::PROGRESS_FOUR],
                'content' => Item::PROGRESS_FOUR,
                'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                'ip' => request()->ip()
            ];
            ItemLog::create($data); //操作日志

            //start流程
            $scheme = Scheme::find($data_budget['flow_id']);
            $serial_no = $this->getnumber($scheme['flowcode']);
            $this->flow = new FlowEngine($scheme['flowcode']);
            $config = [
                'instancecode' => $serial_no,
                'start_time' => $data_budget['start_time'],
                'content' => $model['content'],
                'from_id' => $id,
                'from_table' => 'budget'
            ];
            $this->flow->start($config);
            //end流程
            Budget::update(['status' => 1, 'id' => $id,'project_budget_id'=>$project_budget->id]);
            return json(['code' => 200, 'url' => '', 'msg' => '项目预算填写成功！请耐心等到BOSS审核！']);
        }
        $time = explode('~', $model->period);
        $start_time = $time[0] ?? '';
        $end_time = $time[1] ?? '';
        View::assign('model', $model);
        View::assign('plan', $plan);
        View::assign('flow', $flow);
        View::assign('start_time', $start_time);
        View::assign('end_time', $end_time);
        return View::fetch('budget');//预算
    }

    //预算审核（通过或者拒绝）
    public function ExamineBudget(Request $request, $id)
    {
        $model = Item::with(['ProjectBudget' => ['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig']])->find($id);
        if ($request->isAjax()) {
            $type = $request->param('type'); //类型
            $budget_id = $request->param('budget_id');
            $annotate = $request->param('annotate');
            //更新预算主表批注
            $res = ProjectBudget::update(['annotate' => $annotate, 'id' => $budget_id]);
            if ($res) {
                if ($type == "pass") {
                    $progress = Item::PROGRESS_SIX;

                } else {
                    $progress = Item::PROGRESS_FIVE;

                }
                //更新项目表状态
                Item::update(['progress' => $progress, 'final_admin_id' => $this->auth->id, 'id' => $id]);
                $data = [
                    'name' => $model->ProjectBudget->name,
                    'project_id' => $id,
                    'create_admin_id' => $this->auth->id,
                    'title' => Item::$progress[$progress],
                    'content' => $progress,
                    'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                    'ip' => request()->ip()
                ];
                ItemLog::create($data); //操作日志
                return json(['code' => 200, 'url' => '', 'msg' => '项目审核' . Item::$progress[$progress] . ',加油！']);
            }
        }
        View::assign('model', $model);
        return View::fetch();
    }

    //查看详情
    public function ExamineBudgetDetail($id)
    {

        $bu = Budget::with(['pb'=>['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig'],'project'])->find($id)->toArray();

        if (is_null($bu)) {
            return "暂未找到项目";
        }
      //  $model = Item::with(['ProjectBudget' => ['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig']])->find($bu['project_id']);
        View::assign('model', $bu);
        return View::fetch();
    }


    public function ExamineBudgetEdit(Request $request,$id){
        $bu = Budget::with(['pb'=>['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig'],'project'])->find($id)->toArray();
        if (is_null($bu)) {
            return "暂未找到项目";
        }
        $plan = ProjectPlan::where('status', 1)->select(); //查询计划

        if($request->isAjax()){
            $data = $request->param();
            //1 得到预算表的值
            $res = Budget::with(['pb'=>['admin']])->find($data['id'])->toArray();
            $project_budget_id = $res['project_budget_id'] ?? 0;
            $data_budget = $request->only(['name', 'executive', 'execution_area', 'start_time', 'end_time', 'sample_num', 'flow_id']);
            $data_budget['id'] = $project_budget_id;
            $data_budget['executive_id'] = $res['examine_admin_id'];
            $data_budget['examine_admin_id'] = $this->auth->id; //审核人员
            $data_budget['executive'] =$res['pb']['admin']['nickname'] ?? '';
            $data_budget['project_id'] = $res['project_id'] ?? 0;;
            $data_budget['annotate'] = null;

            if ($data_budget) {
                ProjectBudget::update($data_budget); //更新主表
               // Item::update($data);//更新项目表
            }
            //预算类型配置表
            $type_name = array_filter($request->param('type_name'));
            $main_sample_num = array_filter($request->param('main_sample_num'));
            $enlarge_sample_num = array_filter($request->param('enlarge_sample_num'));
            $type_config = new ProjectBudgetTypeConfig();
            $type_config_data = []; //组合新的数组
            foreach ($type_name as $key => $value) {
                $type_config_data[$key]['project_id'] = $id;
                $type_config_data[$key]['project_budget_id'] = $project_budget_id; //项目预算id
                $type_config_data[$key]['type_name'] = $value ?? '';
                $type_config_data[$key]['main_sample_num'] = $main_sample_num[$key] ?? '';
                $type_config_data[$key]['enlarge_sample_num'] = $enlarge_sample_num[$key] ?? '';
            }
            if ($type_config_data) {
                $res_del = ProjectBudgetTypeConfig::destroy(function ($query) use ($project_budget_id) {
                    $query->where('project_budget_id', $project_budget_id);
                }); //先删除
                if ($res_del) {
                    $type_config->saveAll($type_config_data); //在新增
                }
            }
            //预算成本计划配置表
            $cost_id = array_filter($request->param('cost_id'));
            $num = array_filter($request->param('num'));
            $unit_price = array_filter($request->param('unit_price'));
            $sum_of_money = array_filter($request->param('sum_of_money'));
            $remark = array_filter($request->param('remark'));
            $plan_config = new ProjectBudgetPlanConfig();
            $plan_config_data = [];
            foreach ($cost_id as $key_play => $value_plan) {
                $plan_config_data[$key_play]['project_id'] = $id ?? '';
                $plan_config_data[$key_play]['project_budget_id'] = $project_budget_id; //项目预算id
                $plan_config_data[$key_play]['cost_id'] = $value_plan ?? '';
                $plan_config_data[$key_play]['num'] = $num[$key_play] ?? '';
                $plan_config_data[$key_play]['unit_price'] = $unit_price[$key_play] ?? '';
                $plan_config_data[$key_play]['sum_of_money'] = $sum_of_money[$key_play] ?? '';
                $plan_config_data[$key_play]['remark'] = $remark[$key_play] ?? '';
            }
            if ($plan_config_data) {
                $res_del = ProjectBudgetPlanConfig::destroy(function ($query) use ($project_budget_id) {
                    $query->where('project_budget_id', $project_budget_id);
                }); //先删除
                if ($res_del) {
                    $plan_config->saveAll($plan_config_data);
                }
            }

            return json(['code' => 200, 'url' => '', 'msg' => '项目预算编辑成功！']);

        }
        //  $model = Item::with(['ProjectBudget' => ['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig']])->find($bu['project_id']);
        View::assign('model', $bu);
        View::assign('plan', $plan);
        return View::fetch();
    }




    //重新预算
    public function AgainBudget(Request $request, $id)
    {
        $model = Budget::with(['pb'=>['ProjectBudgetPlanConfig' => ['ProjectPlan'], 'ProjectBudgetTypeConfig','admin'],'project'])->find($id)->toArray();
        $plan = ProjectPlan::where('status', 1)->select(); //查询计划
        if ($request->isAjax()) {

            $data = [
                'id' => $request->param('id'),
                'name' => $request->param('name'),
                'period' => $request->param('start_time') . '~' . $request->param('end_time'), //拼接日期范围
            ];

            //预算主表
            $res = Budget::with(['pb'=>['admin']])->find($data['id'])->toArray();
            $project_budget_id = $res['project_budget_id'] ?? 0;
            $data_budget = $request->only(['name', 'executive', 'execution_area', 'start_time', 'end_time', 'sample_num']);

            $data_budget['id'] = $project_budget_id;
            $data_budget['executive_id'] = $model['examine_admin_id'];
            $data_budget['examine_admin_id'] = $this->auth->id; //审核人员
            $data_budget['executive'] =$res['pb']['admin']['nickname'] ?? '';
            $data_budget['project_id'] = $res['project'] ?? 0;
            $data_budget['annotate'] = null;
            if ($data_budget) {
                ProjectBudget::update($data_budget); //更新主表
//                Item::update($data);//更新项目表
            }

            //预算类型配置表
            $type_name = array_filter($request->param('type_name'));
            $main_sample_num = array_filter($request->param('main_sample_num'));
            $enlarge_sample_num = array_filter($request->param('enlarge_sample_num'));
            $type_config = new ProjectBudgetTypeConfig();
            $type_config_data = []; //组合新的数组
            foreach ($type_name as $key => $value) {
                $type_config_data[$key]['project_id'] = $id;
                $type_config_data[$key]['project_budget_id'] = $project_budget_id; //项目预算id
                $type_config_data[$key]['type_name'] = $value ?? '';
                $type_config_data[$key]['main_sample_num'] = $main_sample_num[$key] ?? '';
                $type_config_data[$key]['enlarge_sample_num'] = $enlarge_sample_num[$key] ?? '';
            }
            if ($type_config_data) {
                $res_del = ProjectBudgetTypeConfig::destroy(function ($query) use ($project_budget_id) {
                    $query->where('project_budget_id', $project_budget_id);
                }); //先删除
                if ($res_del) {
                    $type_config->saveAll($type_config_data); //在新增
                }
            }

            //预算成本计划配置表
            $cost_id = array_filter($request->param('cost_id'));
            $num = array_filter($request->param('num'));
            $unit_price = array_filter($request->param('unit_price'));
            $sum_of_money = array_filter($request->param('sum_of_money'));
            $remark = array_filter($request->param('remark'));
            $plan_config = new ProjectBudgetPlanConfig();
            $plan_config_data = [];
            foreach ($cost_id as $key_play => $value_plan) {
                $plan_config_data[$key_play]['project_id'] = $id ?? '';
                $plan_config_data[$key_play]['project_budget_id'] = $project_budget_id; //项目预算id
                $plan_config_data[$key_play]['cost_id'] = $value_plan ?? '';
                $plan_config_data[$key_play]['num'] = $num[$key_play] ?? '';
                $plan_config_data[$key_play]['unit_price'] = $unit_price[$key_play] ?? '';
                $plan_config_data[$key_play]['sum_of_money'] = $num[$key_play] * $unit_price[$key_play] ?? '';
                $plan_config_data[$key_play]['remark'] = $remark[$key_play] ?? '';
            }
            if ($plan_config_data) {
                $res_del = ProjectBudgetPlanConfig::destroy(function ($query) use ($project_budget_id) {
                    $query->where('project_budget_id', $project_budget_id);
                }); //先删除
                if ($res_del) {
                    $plan_config->saveAll($plan_config_data);
                }
            }
            //更新项目表状态
            Item::update(['progress' => Item::PROGRESS_FOUR, 'examine_admin_id' => $this->auth->id, 'id' => $res['project_id']]);
            $data = [
                'name' => $res['name'] ?? '',
                'project_id' => $res['project_id'] ?? 0,
                'create_admin_id' => $this->auth->id,
                'title' => "BOSS不通过重新提交",
                'content' => Item::PROGRESS_FOUR,
                'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
                'ip' => request()->ip()
            ];
            ItemLog::create($data); //操作日志

            //start流程
            $scheme = Scheme::find($res['flow_id']);
            $serial_no = $this->getnumber($scheme['flowcode']);
            $this->flow = new FlowEngine($scheme['flowcode']);
            $config = [
                'instancecode' => $serial_no,
                'start_time' => $data_budget['start_time'],
                'content' => $model['content'],
                'from_id' => $id,
                'from_table' => 'budget'
            ];
            $this->flow->start($config);
            //end流程
            Budget::update(['status' => 1, 'id' => $id]);


            return json(['code' => 200, 'url' => '', 'msg' => '项目预算重新提交成功！请耐心等到BOSS审核！']);
        }
        View::assign('model', $model);
        View::assign('plan', $plan);
        return View::fetch();
    }

    /**
     * @param Request $request
     * @User: 刘海龙
     * @Date: 2021/7/16
     * @Time: 9:10
     * @return \think\response\Json
     * 新增
     */
    public function ProjectPlan(Request $request){
        $data = $request->param('value');
        $plan_id = ProjectPlan::create(['name'=>$data,'status'=>1])->id;
        return json(['code' => 200, 'msg' => '', 'data' => ['plan_id'=>$plan_id]]);
    }

    //删除项目配置
    public function del_config(Request $request)
    {
        $id = $request->param('id');
        $type = $request->param('type');
        if ($type == "type") {
            ProjectBudgetTypeConfig::destroy($id);
        } else if ($type == "plan") {
            ProjectBudgetPlanConfig::destroy($id);
        }
        return json(['code' => 200, 'url' => '', 'msg' => '删除成功！']);
    }

    /**根据角色获取用户
     * @param $role
     * @return mixed
     */
    public function getuserbyrole($role)
    {
        $sql = "SELECT a.id FROM admins a 
                LEFT JOIN auth_group_access b ON a.id = b.uid
                LEFT JOIN auth_groups c ON b.group_id=c.id
                WHERE c.`id`='" . $role . "'";
        $user = Db::query($sql);
        return $user;
    }
}
